﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：合同管理	
 */
var refreshGirdData; // 更新数据
var categoryId;
var keyword;
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var userinfo;

    //1 查看 2修改 3 下载  4 删除  5 复原
    var btnName = {
        '1': '查看',
        '2': '修改',
        // '3': '下载',
        '4': '删除'
    }

    var page = {
        init: function () {
            page.initGrid();
            page.bind();
            userinfo = learun.clientdata.get(['userinfo']);
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    keyword = $this.val();
                    page.search();
                }
            });
            // 左侧数据加载
            $('#lr_left_tree').lrtree({
                url: config.base_server() + 'dictDatas/tree?access_token=' + config.getToken().access_token,
                param: {code: 'AgreementCategorys'},
                nodeClick: function (item) {
                    categoryId = item.id;
                    $('#titleinfo').text(item.text);
                    page.search();
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                keyword = $('#txt_Keyword').val();
                page.search();
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'Form',
                    title: '合同新增',
                    url: config.webportal_server + 'pages/system/agreement/form.html?categoryId=' + categoryId + '&access_token=' + config.getToken().access_token,
                    width: 800,
                    height: 750,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '合同编辑',
                        url: config.webportal_server + 'pages/system/agreement/form.html?access_token=' + config.getToken().access_token + '&categoryId=' + categoryId + "&keyValue=" + keyValue,
                        width: 800,
                        height: 750,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'agreements/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            //文件授权
            $('#lr_share').on('click', function () {
                var keyValue = $("#gridtable").jfGridValue("id");
                var createUserid = $("#gridtable").jfGridValue("createUserid");

                if (createUserid != userinfo.user.id) {
                    learun.alert.error('您无权分享，请联系管理员');
                    return;
                }
                // if (fileType == 'folder') {
                //     learun.alert.error('请选择要授权的文件');
                //     return;
                // }
                if (keyValue) {
                    learun.layerForm({
                        id: 'fileshare',
                        title: '文件共享',
                        url: config.webportal_server + 'pages/system/agreement/share.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 750,
                        height: 500,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                } else {
                    learun.alert.warning('请选择要共享的文件夹或文件！');
                }
            });
            // 预览表单
            $('#lr_view').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form_PreviewForm',
                        title: '预览合同',
                        url: config.webportal_server + 'pages/system/agreement/previewForm.html?schemeInfoId=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 1024,
                        height: 768,
                        maxmin: true,
                        btn: null
                    });
                }
            });

            /*分类管理*/
            $('#lr_category').on('click', function () {
                learun.layerForm({
                    id: 'ClassifyIndex',
                    title: '分类管理',
                    url: config.webportal_server + 'pages/system/dict/detailIndex.html?access_token=' + config.getToken().access_token + '&itemCode=AgreementCategorys',
                    width: 800,
                    height: 500,
                    maxmin: true,
                    btn: null,
                    end: function () {
                        learun.clientdata.update('dataItem');
                        //location.reload();
                    }
                });
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'agreements/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "编号", name: "number", width: 100, align: "left"},
                    {label: "名称", name: "title", width: 220, align: "left"},
                    {
                        label: "分类", name: "category", width: 100, align: "center",
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'AgreementCategorys',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {label: "对方单位", name: "otherUnits", width: 150, align: "left"},
                    {label: "合同金额", name: "money", width: 120, align: "right",
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2);
                        }
                    },
                    {label: "起始时间", name: "startTime", width: 110, align: "center",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd');
                        }
                    },
                    {label: "结束时间", name: "endTime", width: 110, align: "center",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd');
                        }
                    },
                    {label: "创建人", name: "createUsername", width: 100, align: "center"},
                    {label: "创建时间", name: "createTime", width: 150, align: "center",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "摘要", name: "summary", width: 200, align: "left"},
                    {label: '操作', name: 'id', width: 200, align: 'left',
                        formatter: function (value, row, op, $cell) {
                            var $div = $('<div></div>');
                            var btnlist = row.authType.split(',');
                            $.each(btnlist, function (_index, _item) {
                                if ($div.find('[data-value="' + _item + '"]').length == 0) {
                                    var btnClass = 'label-info';
                                    if (_item == '4') {
                                        btnClass = 'label-warning';
                                    }
                                    var $btn = $('<span class=\"label ' + btnClass + '\" data-value="' + _item + '" style=\"cursor: pointer; margin-right:8px;\">' + btnName[_item] + '</span>');
                                    $btn[0]._row = row;
                                    $btn.on('click', function () {
                                        var $this = $(this);
                                        var btnValue = $this.attr('data-value');
                                        var btnRow = $this[0]._row;
                                        switch (btnValue) {
                                            case '1':// 查看
                                                selectedRow = row;
                                                learun.layerForm({
                                                    id: 'Form',
                                                    title: '合同查看',
                                                    url: config.webportal_server + 'pages/system/agreement/view.html?access_token=' + config.getToken().access_token + '&categoryId=' + categoryId + "&keyValue=" + row.id,
                                                    width: 800,
                                                    height: 750,
                                                    btn: []
                                                });
                                                break;
                                            case '2':// 编辑
                                                selectedRow = row;
                                                learun.layerForm({
                                                    id: 'Form',
                                                    title: '合同编辑',
                                                    url: config.webportal_server + 'pages/system/agreement/form.html?access_token=' + config.getToken().access_token + '&categoryId=' + categoryId + "&keyValue=" + row.id,
                                                    width: 800,
                                                    height: 750,
                                                    callBack: function (id) {
                                                        return top[id].acceptClick(refreshGirdData);
                                                    }
                                                });
                                                break;
                                            case '4':// 删除
                                                learun.layerConfirm('是否确认删除该合同！', function (res) {
                                                    if (res) {
                                                        learun.deleteForm(config.base_server() + 'agreements/delete/' + btnRow.id + '?access_token=' + config.getToken().access_token, {keyValue: row.id}, function () {
                                                            refreshGirdData();
                                                        });
                                                    }
                                                });
                                                break;
                                        }
                                    });
                                    $div.append($btn);
                                }
                            });
                            return $div;
                        }
                    }
                ],
                dblclick: function (row) {
                    selectedRow = row;
                    var keyValue = row.id;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'Form',
                            title: '合同编辑',
                            url: config.webportal_server + 'pages/system/agreement/form.html?access_token=' + config.getToken().access_token + '&categoryId=' + categoryId + "&keyValue=" + keyValue,
                            width: 800,
                            height: 800,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                mainId: 'id',
                reloadSelected: true,
                isPage: true
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.category = categoryId;
            param.keyword = keyword;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}